<?php

/**
 * 好物清单- 清洗历史清单已关联商品到wiki_list(已关联 商品表)
 */

ini_set('display_errors', 1);
ini_set('memory_limit', -1); //不限制内存
ini_set("error_reporting", E_ALL);//显示所有错误

require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\Album\AlbumWikiListModel;
use Biz\model\wiki\WikiList as mWikiList;
use Biz\module\Classes\wiki\RelateWiki;

//1、查询好物清单正常关联的商品列表
$wikiList = AlbumWikiListModel::selectByWhere([
    'status' => AlbumWikiListModel::STATUS_NORMAL,
]);
$uidArr = array_column($wikiList, 'uid');
if ($wikiList) {
    foreach ($wikiList as $wikiInfo) {
        $uid = $wikiInfo['uid'] ?? 0;
        $wikiId = $wikiInfo['wiki_id'] ?? 0;
        if (0 == $uid || 0 == $wikiId) {
            continue;
        }
        //写入wiki_list
        $has = mWikiList::has_master(['uid' => $uid, "wiki_id" => $wikiId]);
        if ($has) {
            mWikiList::updateData(['addtime' => date('Y-m-d H:i:s'), 'status' => mWikiList::STATUS_NORMAL],
                ['uid' => $uid, 'wiki_id' => $wikiId]);
        } else {
            mWikiList::insertData([
                'uid' => $uid,
                'wiki_id' => $wikiId,
                'addtime' => date('Y-m-d H:i:s'),
                'status' => mWikiList::STATUS_NORMAL,
            ]);
        }
    }
    foreach ($uidArr as $uid) {
        //更新用户wiki清单
        if($uid >0){
            RelateWiki::changeUserWikiList($uid);
        }
    }


}
